package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.api.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.small.domin.Goods;
import cn.wolfcode.luowowo.small.query.GoodsQueryObject;
import cn.wolfcode.luowowo.small.service.IGoodsService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/small")
public class SmallController {
    @Reference
    private IGoodsService goodsService;
    @Reference
    IUserInfoRedisService userInfoRedisService;
    @Reference
    private IUserInfoService userInfoService;
    /*  @RequestMapping("/list")
      public String list(Model model, @ModelAttribute("qo") GoodsQueryObject qo){
          PageInfo pageInfo = goodsService.query(qo);
          model.addAttribute("pageInfo",pageInfo);
          return "small/market";
      }
    @RequestMapping("/intro")
    public String intro(Model model, @ModelAttribute("qo") GoodsQueryObject qo){
        PageInfo pageInfo = goodsService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "small/intro";*/


    @RequestMapping("/list")
    @RequireLogin
    public String list(@UserParam UserInfo userInfo, Model model, @ModelAttribute("qo") GoodsQueryObject qo){
        PageInfo pageInfo = goodsService.query(qo);
        Long coin=userInfoService.getCoinByUserId(userInfo.getId());
        //如果查到的金币为空返回0
        if(coin==null){
            model.addAttribute("mycoin",0);
        }else {
            model.addAttribute("mycoin",coin);
        }
        model.addAttribute("pageInfo",pageInfo);

        return "small/market";
    }
    @RequestMapping("/intro")
    public String intro(Model model, Long goodsId,@UserParam UserInfo userInfo){
        Goods goods = goodsService.selectById(goodsId);
        model.addAttribute("userInfo",userInfo);
        model.addAttribute("goods",goods);
        return "small/intro";
    }
    @RequestMapping("/buy")
    @ResponseBody
    public Object buy(@UserParam UserInfo userInfo, Goods goods){
        AjaxResult result= new AjaxResult();
        //获取用户的金币
        Long userCoin=userInfoService.getCoinByUserId(userInfo.getId());
        //判断如果有用户金币不为空且大于购买道具所需金币则扣钱
        if(userCoin!=null&&userCoin>=goods.getGold()&&goods.getNum()>0){
            Long gold = goods.getGold();
            Long coin=userCoin-goods.getGold();
            Long num = goods.getNum();
            //扣用户帐户里的金币
            userInfoService.updateCoinByUserId(userInfo.getId(),coin);
            //同时道具的数量减一
            goodsService.updateGoodsNumById(goods.getId());
            //维护关系表
            goodsService.addUserAndGoodsRelationById(goods.getId(),userInfo.getId());
            result.setSuccess(true);
            result.setMsg("购买成功");
        }else {
            result.setMsg("你的金币余额不足,请充值");
        }
        return  result;
    }

    @RequestMapping("/tab")

    public String tab(@UserParam UserInfo userInfo,Model model, @ModelAttribute("qo") GoodsQueryObject qo){
        PageInfo pageInfo = goodsService.query(qo);
        if(userInfo==null){
            throw new RuntimeException("请先登陆!!!!");
        }
        //查出用户所有的积分
        Long userCoin = userInfoService.getCoinByUserId(userInfo.getId());
        //查出用户所有的道具
        List<Goods> tabeGoods=userInfoService.getGoodsByUserId(userInfo.getId());
            model.addAttribute("tabeGoods",tabeGoods);
        model.addAttribute("pageInfo",pageInfo);
        if(userCoin==null){
            model.addAttribute("mycoin",0);
        }else {
            model.addAttribute("mycoin",userCoin);
        }
        return "small/goodstab";

    }


    @RequestMapping("/addcoin")
    public Object addcoin(Model model, Integer coin,@UserParam UserInfo userInfo){
        Long longcoin=Long.valueOf(coin);
        AjaxResult result= new AjaxResult();
       try {
           if(userInfo.getId()!=null){
               userInfoService.addUserCoinById(userInfo.getId(),longcoin);
               result.setSuccess(true);
               result.setMsg("已经成功充值"+coin+"金币到你的帐户中");
           }
       }catch (Exception e){
           e.printStackTrace();
           result.setMsg("充值失败");
       }



        return result;
    }
}
